package com.yongqi.duocai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yongqi.duocai.entity.Address;
import com.yongqi.duocai.mapper.AddressMapper;
import com.yongqi.duocai.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 夏亮辉
 * @since 2021-06-22
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    /**
     * 查询当前用户 的所有地址信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<Address> queryByUserId(Integer userId) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return addressMapper.selectList(queryWrapper);
    }

    /**
     * 查找用户的默认地址
     *
     * @param userId
     * @return
     */
    @Override
    public Address queryDefault(Integer userId) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId)
                    .eq("is_default", 1);
        return addressMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询
     * @param address
     * @return
     */
    @Override
    public List<Address> queryOne(Address address) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", address.getUserId())
                    .eq("buyer", address.getBuyer())
                    .eq("phone", address.getPhone())
                    .eq("area", address.getArea())
                    .eq("address", address.getAddress());
        return addressMapper.selectList(queryWrapper);
    }

    /**
     * 根据 id 查询
     *
     * @param id
     * @return
     */
    @Override
    public Address getOneById(Integer id) {
        return addressMapper.selectOneById(id);
    }
}
